﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Browser;
using AnalysisApp.Silverlight.ServiceReference1;
using Ranet.AgOlap;
using Ranet.AgOlap.Controls.DynamicPivotGrid;
using Ranet.AgOlap.Controls.MdxDesigner;
using Ranet.AgOlap.Controls.MdxDesigner.Wrappers;
using Ranet.AgOlap.Controls.MemberChoice.Info;
using Ranet.AgOlap.UI.DynamicPivotGrid;
using Ranet.Olap.Core;
using Ranet.Olap.Core.Data;
using Ranet.Olap.Core.Metadata;
using Ranet.Olap.Core.Storage;
using Ranet.Olap.Core.Types;

namespace AnalysisApp.Silverlight
{
    /// <summary>
    /// Containes methodes which can be called from MainPage
    /// </summary>
    [ScriptableType]
    public partial class MainPage
    {
        /// <summary>
        /// Creates a connection inside the connection pool
        /// </summary>
        /// <param name="connection"></param>
        [ScriptableMember]
        public void SetConnectionString(string connection)
        {
            ServiceReference1.OlapWebServiceSoapClient client = new OlapWebServiceSoapClient();
            client.PerformOlapServiceActionAsync("SETCONNECTIONSTRING", connection);
        }

        [ScriptableMember]
        public void Init(string cube)
        {

            ServiceReference1.OlapWebServiceSoapClient client = new OlapWebServiceSoapClient();
            client.PerformOlapServiceActionCompleted += new EventHandler<PerformOlapServiceActionCompletedEventArgs>((sender, e) =>
                                                                                                                         {
                                                                                                                             
                                                                                                                             pivotMdxDynamicControl.Connection = e.Result;
                                                                                                                             pivotMdxDynamicControl.AutoExecuteQuery = false;
                                                                                                                             pivotMdxDynamicControl.CubeName=cube;
                                                                                                                             pivotMdxDynamicControl.UseCellConditionsDesigner = true;
                                                                                                                             pivotMdxDynamicControl.IsShowStateBar = true;
                                                                                                                             pivotMdxDynamicControl.Initialize();
                                                                                                                         });
            client.PerformOlapServiceActionAsync("GETCONNECTIONSTRING", "OK");
        }

        [ScriptableMember]
        public void SetMdxDesignerSettings(int subsetCount,
                                           bool useVisualTotals,
                                           bool generateCustomCalculated,
                                           bool hideEmptyColumns,
                                           bool hideEmptyRows,
                                           bool includeAllMembers,
                                           bool loadQueryState,
                                           bool isImportMDXQuery)
        {

            MDXDesignerSettingWrapper mdxDesignerSetting=new MDXDesignerSettingWrapper()
                                                             {
                                                                SubsetCount = subsetCount, GenerateCustomCalculated = generateCustomCalculated,HideEmptyColumns = hideEmptyColumns,HideEmptyRows = hideEmptyRows,
                                                                 IncludeAllMembers = includeAllMembers,IsImportMDXQuery = isImportMDXQuery,IsUsed = true
                                                             };


            //HideEmptyColumnsButton.IsChecked = mdxDesignerSetting.HideEmptyColumns;
            //HideEmptyRowsButton.IsChecked = mdxDesignerSetting.HideEmptyRows;

            
            //обновить текущую вкладку с учетом настроек

            pivotMdxDynamicControl.DesignerSetting = mdxDesignerSetting;
            pivotMdxDynamicControl.ExportDesignerSetting();
            QueryDesignerControl currentDesigner = pivotMdxDynamicControl.CurrentDesigner;
            
            if (currentDesigner != null)
            {
                currentDesigner.m_AreasDesigner.m_DesignerSetting = mdxDesignerSetting;
                //обновить параметры грида с учетом настроек
                currentDesigner.UpdateablePivotGrid.IsVisibleEmptyColumns = mdxDesignerSetting.HideEmptyColumns;
                currentDesigner.UpdateablePivotGrid.IsVisibleEmptyRows = mdxDesignerSetting.HideEmptyRows;
                currentDesigner.UpdateablePivotGrid.AxisSubsetCount = mdxDesignerSetting.SubsetCount;
                //обновить MDX запрос и все области результата
                currentDesigner.RefreshMdxUpdateOutputAreas();
            }

            pivotMdxDynamicControl.ExportDesignerSetting();

        }

    }
}
